﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;

using System.Diagnostics;

namespace doris.Admin {
    public partial class POP3Edit : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (Request.QueryString["id"] == null) {
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
            }
      /*         if (Request.QueryString["id"] == null) return;
                  MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
                  MySqlCommand cmd = new MySqlCommand();
                  cmd.Connection = conn;

                  cmd.CommandText = "getPop3";
                  cmd.CommandType = CommandType.StoredProcedure;

                  cmd.Parameters.AddWithValue("@in_configurationmailid", MySqlDbType.Int32);
                              cmd.Parameters["@in_configurationmailid"].Direction = ParameterDirection.Input;
                              cmd.Parameters["@in_configurationmailid"].Value = int.Parse(Request.QueryString["id"]);

                  try {
                      conn.Open();
                      //                cmd.ExecuteNonQuery();

                      MySqlDataReader reader = cmd.ExecuteReader();
                      //                int rownr = 1;
                      //                while (reader.Read()) {
                      //                   Debug.WriteLine("Row {0}: {1} {2}",(rownr++),reader["data_configurationmailid"],reader["data_connectionname"]);
                      //                }

                      DetailsView1.DataSource = reader;
                      DetailsView1.DataBind();
                  } finally {
                      conn.Close();
                  }*/

        }
        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) {
            if (e.NewMode == DetailsViewMode.Edit) {
                DetailsView1.ChangeMode(DetailsViewMode.Edit);
            } else if (e.NewMode == DetailsViewMode.ReadOnly) {
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            } else if (e.NewMode == DetailsViewMode.Insert) {
/*                int id = Convert.ToInt32(((Label)DetailsView1.FindControl("IdLabel")).Text);
                string name = ((TextBox)FormView1.FindControl("NameTextBox")).Text;
                string city = ((TextBox)FormView1.FindControl("CityTextBox")).Text;
                string state = ((TextBox)FormView1.FindControl("StateTextBox")).Text;
                Employee.UpdateEmployee(id, name, city, state);*/
            }
        }
        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) {
            Response.Redirect("MailConfig.aspx");
        }
        protected void DetailsView1_ItemUpdating(object sender, SqlDataSourceCommandEventArgs e) {
 /*           for (int i = 0; i < e.NewValues.Count; i++) {
                if (e.NewValues[i] != null)
                    e.NewValues[i] = Server.HtmlEncode(e.NewValues[i].ToString());
            }*/

            CheckBox cb = (CheckBox)DetailsView1.FindControl("cbUseCredentials");
            CheckBox cbS = (CheckBox)DetailsView1.FindControl("cbUseSSL");
            CheckBox cbE = (CheckBox)DetailsView1.FindControl("cbEnabled");

            // putt inn alle 
            e.Command.Parameters["@in_server"].Value = e.Command.Parameters["@Server"].Value;
            e.Command.Parameters["@in_configurationmailid"].Value = e.Command.Parameters["@ConfigurationMailID"].Value;
            e.Command.Parameters["@in_connectionname"].Value = e.Command.Parameters["@ConnectionName"].Value;
            e.Command.Parameters["@in_usecredentials"].Value = 1;
            e.Command.Parameters["@in_username"].Value = e.Command.Parameters["@Username"].Value;
            e.Command.Parameters["@in_password"].Value = e.Command.Parameters["@Password"].Value;
            e.Command.Parameters["@in_port"].Value = e.Command.Parameters["@Port"].Value;
            e.Command.Parameters["@in_usessl"].Value = (cbS.Checked ? "1" : "0");
            e.Command.Parameters["@in_enabled"].Value = (cbE.Checked ? "1" : "0");
            e.Command.Parameters["@in_email"].Value = e.Command.Parameters["@Email"].Value;

            e.Command.Parameters.Remove(e.Command.Parameters["@Server"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConfigurationMailID"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConnectionName"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Username"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Password"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Port"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@UseSSL"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Enabled"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Email"]);
            /*
                                    MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
                                    MySqlCommand cmd = new MySqlCommand();
                                    cmd.Connection = conn;

                                    cmd.CommandText = "storePop3";
                                    cmd.CommandType = CommandType.StoredProcedure;

                                    cmd.Parameters.AddWithValue("@in_configurationmailid", int.Parse(Request.QueryString["id"]));
                        //            cmd.Parameters["@in_configurationmailid"].MySqlDbType = MySqlDbType.Int32;
                        //            cmd.Parameters["@in_configurationmailid"].Direction = ParameterDirection.Input;

                                    cmd.Parameters.AddWithValue("@in_server", e.NewValues["Server"].ToString());
                                    cmd.Parameters.AddWithValue("@in_connectionname", e.NewValues["ConnectionName"].ToString());
                                    Debug.WriteLine(e.NewValues["ConnectionName"].ToString());

                                    try {
                                        conn.Open();
                                        cmd.ExecuteNonQuery();
                                    } finally {
                                        conn.Close();
                                    }
                                    */
        }
        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) {
            CheckBox cbS = (CheckBox)DetailsView1.FindControl("cbUseSSL");
            CheckBox cbE = (CheckBox)DetailsView1.FindControl("cbEnabled");

            // putt inn alle 
            e.Command.Parameters["@in_server"].Value = e.Command.Parameters["@Server"].Value;
            e.Command.Parameters["@in_connectionname"].Value = e.Command.Parameters["@ConnectionName"].Value;
            e.Command.Parameters["@in_usecredentials"].Value = 1;
            e.Command.Parameters["@in_username"].Value = e.Command.Parameters["@Username"].Value;
            e.Command.Parameters["@in_password"].Value = e.Command.Parameters["@Password"].Value;
            e.Command.Parameters["@in_port"].Value = e.Command.Parameters["@Port"].Value;
            e.Command.Parameters["@in_usessl"].Value = (cbS.Checked ? "1" : "0");
            e.Command.Parameters["@in_enabled"].Value = (cbE.Checked ? "1" : "0");
            e.Command.Parameters["@in_email"].Value = e.Command.Parameters["@Email"].Value;

            e.Command.Parameters.Remove(e.Command.Parameters["@Email"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Server"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConnectionName"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Username"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Password"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Port"]);
//            e.Command.Parameters.Remove(e.Command.Parameters["@UseSSL"]);
//            e.Command.Parameters.Remove(e.Command.Parameters["@Enabled"]);

        }
    }
}